<?php defined( '_VALID_MOS' ) or die( include("404.php") );
switch($_POST["hidden"])
{
	case "";
	// khoi dau trang khong co gia tri submit. khong lam zi ca
	break;
	
	/* khoi su ly su kien submit form them logo */
	case "submit_com_video_add";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_addvideo($_POST["title"], $_POST["image_file"], $_POST["video_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("video", "order_num")) <> FALSE){
				$core_class->_redirect(".?com=com_video&task=view");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_addvideo($_POST["title"], $_POST["image_file"], $_POST["video_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $myprocess->process_getmaxid("video", "order_num")) <> FALSE){
				$core_class->_redirect(".?com=com_video&task=add");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_video&task=view");
			exit();
		}
	break;
	
	/* khoi su ly su kien submit form sua muc video */
	case "submit_com_video_edit";
		if($_POST["task"] == "save"){
			$myprocess = new process;
			if($myprocess->process_editvideo($_POST["title"], $_POST["image_file"], $_POST["video_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_video&task=view");
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "apply"){
			$myprocess = new process;
			if($myprocess->process_editvideo($_POST["title"], $_POST["image_file"], $_POST["video_file"], $core_class->_formatdatetime($_POST["date_add"]), $_POST["published"], $_POST["Id"]) <> FALSE){
				$core_class->_redirect(".?com=com_video&task=edit&id=".$_POST["Id"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Ðã có l?i thêm nhóm logo, vui lòng làm l?i!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_video&task=view");
			exit();
		}
	break;
	
	/* khoi su ly su kien submit form sua muc video */
	case "submit_com_video_view";
		if($_POST["task"] == "unpublish"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_pulish_and_un_publish_video("0", $values[$row]);
			}
			$core_class->_redirect(".?com=com_video&task=view");
		}
		else if($_POST["task"] == "publish"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_pulish_and_un_publish_video("1", $values[$row]);
			}
			$core_class->_redirect(".?com=com_video&task=view");
		}
		else if($_POST["task"] == "orderup"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_orderdownvideo($values[$row]);
			}
			$core_class->_redirect(".?com=com_video&task=view");
		}
		else if($_POST["task"] == "orderdown"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_orderupvideo($values[$row]);
			}
			$core_class->_redirect(".?com=com_video&task=view");
		}
		else if($_POST["task"] == "remove"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				$myprocess->process_remove_video($values[$row]);			
			}
			$core_class->_redirect(".?com=com_video&task=view");
		}
	break;
	
	default:
		$core_class->_redirect(".");exit();
	break;
}

class process{

	// ham su ly di chuyen mau tin xuong phia duoi cua video
	function process_orderdownvideo($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order_num` from video WHERE Id = $psesid) As `currenOrder`, 
		(SELECT `order_num` from video WHERE `order_num` > 
		(SELECT `order_num` from video WHERE Id = $psesid) 
		Order by `order_num` LIMIT 1) As `preOrder`,
		(SELECT Id from video WHERE `order_num` = 
		(SELECT `order_num` from video WHERE `order_num` > 
		(SELECT `order_num` from video WHERE Id = $psesid) 
		Order by `order_num` LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update video set `order_num` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update video set `order_num` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}

	// ham su ly di chuyen mau tin len phia tren cua video
	function process_orderupvideo($psesid){
		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order_num` from video WHERE `Id` = $psesid) As `currenOrder`, 
		(SELECT `order_num` from video WHERE `order_num` < 
		(SELECT `order_num` from video WHERE `Id` = $psesid) 
		Order by `order_num` desc LIMIT 1) As `preOrder`,
		(SELECT `Id` from video WHERE `order_num` = 
		(SELECT `order_num` from video WHERE `order_num` < 
		(SELECT `order_num` from video WHERE `Id` = $psesid) 
		Order by `order_num` desc LIMIT 1)) As `preSesid`";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update video set `order_num` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update video set `order_num` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $psesid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly them video moi
	function process_addvideo($title, $image_path, $video_path, $date_add, $activated, $order_num){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into video(`title`, `image_path`, `file_path`, `date_add`, `activated`, `order_num`)
				VALUES(?,?,?,?,?,?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssss", $title, $image_path, $video_path, $date_add, $activated, $order_num);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();				
	}
	// ham su ly chinh sua muc video
	function process_editvideo($title, $image_file, $video_file, $date_add, $activated, $Id){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "UPDATE video SET `title` = ?, `image_path` = ?, `file_path` = ?, `date_add` = ?, `activated` = ?
				WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssss", $title, $image_file, $video_file, $date_add, $activated, $Id);
		if($cmd->execute() <> FALSE) return true; 
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();				
	}
	// ham su lay so thu tu lon nhat cho moi mau tin
	function process_getmaxid($table, $column){
		include("../protected/dbconnect.php");
		$sql = "select MAX(`$column`)+1 As `MaxId` from `$table`;";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($MaxId);
		if($cmd->fetch()){
			if($MaxId == 0)	return 1;
			else return $MaxId;
		}
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly su kien publish va khong publish video
	function process_pulish_and_un_publish_video($check, $values){
		include("../protected/dbconnect.php");
		if($check == 0)
		$sql = "Update video Set `activated` = 0 Where `Id` = ?";
		else $sql = "Update video Set `activated` = 1 Where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly go bo video
	function process_remove_video($values){
		include("../protected/dbconnect.php");
		$myprocess = new process;
		$sql = "Delete from `video` where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
}